/******************************************************************************\
    Event.hpp discribes a windows event class.
    Copyright (C) 2008  Victor Vinogradov (fly3333@gmail.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
\******************************************************************************/

#ifndef __EVENT_HPP
#define __EVENT_HPP

#include "Handle.hpp"

class cEvent : cObject< cSubHandle >
{
public:
	bool Create( LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCTSTR lpName )
	{		
		HANDLE handle = ::CreateEvent( lpEventAttributes, bManualReset, bInitialState, lpName );
		if( handle == NULL )
			return false;
		cSubHandle *sub_handle;
		try
		{
			sub_handle = new cSubHandle( handle );
		}
		catch( ... )
		{
			return false;
		}
		SwitchToSubObject( sub_handle );
		return true;
	}
	operator HANDLE() const
	{
		if( GetSubObject() == NULL )
			return INVALID_HANDLE_VALUE;
		return GetSubObject()->Handle();
	}
	bool Set()
	{
		if( GetSubObject() == NULL )
			return false;
		return ::SetEvent( GetSubObject()->Handle() ) != FALSE;
	}
	bool Reset()
	{
		if( GetSubObject() == NULL )
			return false;
		return ::ResetEvent( GetSubObject()->Handle() ) != FALSE;
	}
	bool Pulse()
	{
		if( GetSubObject() == NULL )
			return false;
		return ::PulseEvent( GetSubObject()->Handle() ) != FALSE;
	}
};

#endif
